package com.truthbean.jutils.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

public class MailUtils {

    private final static String HOST = "smtp.exmail.qq.com";
    private final static String USERNAME = "hjj@hsdzx.cn";
    private final static String PASSWORD = "hejianjiao2014";
    private final static String FROM = "hjj@hsdzx.cn";
    private final static String FROM_NAME = "湖北师范学院荷尖角技术团队";

    private static final Map<String, String> HOSTMAP = new HashMap<>();

    static {
        // 126
        HOSTMAP.put("smtp.126", "smtp.126.com");
        // qq
        HOSTMAP.put("smtp.qq", "smtp.qq.com");

        // 163
        HOSTMAP.put("smtp.163", "smtp.163.com");

        // sina
        HOSTMAP.put("smtp.sina", "smtp.sina.com.cn");

        // tom
        HOSTMAP.put("smtp.tom", "smtp.tom.com");

        // 263
        HOSTMAP.put("smtp.263", "smtp.263.net");

        // yahoo
        HOSTMAP.put("smtp.yahoo", "smtp.mail.yahoo.com");

        // hotmail
        HOSTMAP.put("smtp.hotmail", "smtp.live.com");

        // gmail
        HOSTMAP.put("smtp.gmail", "smtp.gmail.com");
        HOSTMAP.put("smtp.port.gmail", "465");
    }

    // 发送邮件
    public static void send(String to, String subject, String htmlMsg) {
        if (StringUtil.isNotEmpty(to) && StringUtil.isNotEmpty(subject) && StringUtil.isNotEmpty(htmlMsg)) {
            try {
                HtmlEmail email = new HtmlEmail();
                email.setCharset("utf-8");// 设置编码，以防乱码出现
                email.setHostName(HOST);// 指定SMTP server
                email.setAuthentication(USERNAME, PASSWORD);
                email.addTo(to);// 指定接收方地址与名字
                email.setFrom(FROM, FROM_NAME);// 指定发送方地址与名字
                email.setSubject(subject);// 指定邮件标题
                email.setHtmlMsg(htmlMsg);
                //String template = "尊敬的%s，<br>请点击下面的链接激活账号：<br><font color='red'>%s</font><br>如果不能点击链接，请复制到浏览器地址输入框访问。（该邮件24小时内有效）<br>荷尖角团队";
                //email.setHtmlMsg(String.format(template, USERNAME, link));// 指定邮件内容
                email.send();// 发送
            } catch (EmailException e) {
                e.printStackTrace();
            }
        }
    }

    public static String getHost(String email) throws Exception {
        Pattern pattern = Pattern.compile("\\w+@(\\w+)(\\.\\w+){1,2}");
        Matcher matcher = pattern.matcher(email);
        String key = "unSupportEmail";
        if (matcher.find()) {
            key = "smtp." + matcher.group(1);
        }
        if (HOSTMAP.containsKey(key)) {
            return HOSTMAP.get(key);
        } else {
            throw new Exception("unSupportEmail");
        }
    }

    public static int getSmtpPort(String email) throws Exception {
        Pattern pattern = Pattern.compile("\\w+@(\\w+)(\\.\\w+){1,2}");
        Matcher matcher = pattern.matcher(email);
        String key = "unSupportEmail";
        if (matcher.find()) {
            key = "smtp.port." + matcher.group(1);
        }
        if (HOSTMAP.containsKey(key)) {
            return Integer.parseInt(HOSTMAP.get(key));
        } else {
            return 25;
        }
    }

    /*// 用户找回密码
	public void send(User user) {
		try {
			HtmlEmail email = new HtmlEmail();
			email.setCharset("utf-8");// 设置编码，以防乱码出现
			email.setHostName(HOST);// 指定SMTP server
			email.setAuthentication(USERNAME, PASSWORD);
			email.addTo(user.getEmail());// 指定接收方地址与名字
			email.setFrom(FROM, FROM_NAME);// 指定发送方地址与名字
			email.setSubject(subject);// 指定邮件标题
			email.setHtmlMsg("尊敬的"
					+ user.getUsername()
					+ "，<br>已为你找回密码，你的密码是：<font color='red'>"
					+ user.getPassword()
					+ "</font><br>赶快前去登录吧，<a href='http://www.hsdzx.cn/login.jsp'>点击登录</a><br>荷尖角团队");// 指定邮件内容
			email.send();// 发送
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}*/
}
